from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION

from pptx import Presentation
from pptx.util import Inches
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT

# # 步骤一: 创建一个新的幻灯片文档
prs = Presentation()  # 无参:新建文件  有参:读取文件
slides = prs.slides  # 幻灯片集合(动态的)

# 步骤二: 写入操作
for i in range(10):
    slides.add_slide(prs.slide_layouts[0])  # 往slides里添加空白幻灯片模板; (索引只能是[-11,10]; 不同索引生成的模板不同;6是空白模板)
# del slides._sldIdLst[3]  # 根据索引,删除幻灯片

# ################## 往第1张幻灯片写入内容 start
slide_one = slides[0]  # 第1张幻灯片
my_shape = slide_one.shapes  # 图形
title_shape = my_shape.title
title_shape.text = '标题1'  # 设置标题名称
my_shape.placeholders[1].text = '标题2'

my_text = my_shape.add_textbox(Inches(0), Inches(2), Inches(5), Inches(3))  # 添加文本框
my_text.text = "我是文本框"  # 设置文本框内容
p1 = my_text.text_frame.add_paragraph()  # 添加一个段落
p1.text = '我是段落1'  # 设置段落内容
p1.add_run().text = '\n 结束了end'

# ########################往第4张幻灯片填充图形  start
# 添加自选图形 参考文档:  https://python-pptx.readthedocs.io/en/latest/api/enum/MsoAutoShapeType.html
# MSO_SHAPE.RECTANGLE -->矩形    MSO_SHAPE.HEXAGON -->六边形
shape = slides[4].shapes.add_shape(MSO_SHAPE.HEXAGON, Inches(2), Inches(2), Inches(5), Inches(3))
# 填充边框
fill = shape.fill
fill.solid()
fill.fore_color.rgb = RGBColor(255, 0, 0)
line = shape.line
line.color.rgb = RGBColor(55, 3, 5)
line.width = Pt(2)

# ##################往第3张幻灯片添加表格  start
table = slides[3].shapes.add_table(3, 3, Inches(2), Inches(2), Inches(4), Inches(2)).table
# 填充内容
table.cell(1, 0).text = 'name'
table.cell(1, 1).text = 'age'
table.cell(1, 2).text = 'class'
table.cell(2, 0).text = '张三'
table.cell(2, 1).text = '19'
table.cell(2, 2).text = '一班'
# 合并单元格
cell = table.cell(0, 0)
cell1 = table.cell(0, 2)
cell.merge(cell1)
table.cell(0, 0).text = '班级学生信息'
print(cell.is_merge_origin)
cell.split()  # 取消合并

# #######往第2张幻灯片写入图表  start
chart_data = CategoryChartData()
chart_data.categories = ['一月份', '二月份', '三月份']  # X轴
chart_data.add_series('Y2023', (300, 400, 520))
chart_data.add_series('Y2024', (340, 410, 220))
# XL_CHART_TYPE.COLUMN_CLUSTERED 柱状图 LINE->折线图
chart = slides[2].shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, Inches(2), Inches(2), Inches(6), Inches(4),
                               chart_data).chart
chart.has_title = True
chart.chart_title.text_frame.text = '第一季度销售额'  # 标题
chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.RIGHT

# 步骤三: 保存PPT文件
prs.save('../myFile/ppt/test.pptx')
